package ex.recruit.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import modules.recruit.entity.RmProvince;
import modules.recruit.service.RmProvinceService;

import org.springframework.jdbc.core.RowMapper;

import com.guinea.core.orm.JdbcDao;
import com.guinea.core.tag.Province;

import ex.recruit.vo.RmProvListVoEx;

public class RmProvinceServiceEx extends RmProvinceService {
	JdbcDao jdbcDao;

	public JdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(JdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}
	
	public List findAndGroupManager(List filters){
		List l = getRmProvinceDao().find(filters);
		List result = new ArrayList();
		for (Iterator iter = l.iterator(); iter.hasNext();) {
			RmProvince rmProvince = (RmProvince) iter.next();
			if(rmProvince.getOwner() == null) continue;
			boolean exists = false;
			for (Iterator iterator = result.iterator(); iterator.hasNext();) {
				RmProvListVoEx rmProvListVoEx = (RmProvListVoEx) iterator.next();
				if(rmProvListVoEx.getOwner().longValue() == rmProvince.getOwner().longValue()){
					rmProvListVoEx.getRmProvinces().add(rmProvince);
					exists = true;
					break;
				}
			}
			if(!exists){
				RmProvListVoEx rmProvListVoEx = new RmProvListVoEx();
				rmProvListVoEx.setOwner(rmProvince.getOwner());
				List li = new ArrayList();
				li.add(rmProvince);
				rmProvListVoEx.setRmProvinces(li);
				result.add(rmProvListVoEx);
			}
		}
		return result;
	}
	
	public List getUnallocatedProvinces(List provinces){
		List rps = get();
		List result = new ArrayList();
		for (Iterator iter = provinces.iterator(); iter.hasNext();) {
			Province province = (Province) iter.next();
			String code = province.getCode();
			boolean allocated = false;
			for (Iterator iterator = rps.iterator(); iterator.hasNext();) {
				RmProvince rp = (RmProvince) iterator.next();
				if(code.equals(rp.getProvinceId())){
					allocated = true;
					break;
				}
			}
			if(!allocated)
				result.add(province);
		}
		return result;
	}

	public List getManagers() {
		List l = getJdbcDao()
				.getJdbcTemplate()
				.query(
						"select * from sk_rm_province r left join sk_user u on r.owner = u.id where r.status = '1' and u.status = '1'",
						new RowMapper() {
							public Object mapRow(ResultSet rs, int i)
									throws SQLException {

								return null;
							}
						});

		return null;
	}
}
